const modulesMap = {};

function call({ type, data }) {
  const [module, method] = type.split("/");
  const cloudObj = modulesMap[module] || uniCloud.importObject(module);
  modulesMap[module] = cloudObj;
  const requestData = {
    token: uni.getStorageSync("TOKEN"),
    data: data,
  };
  if (!cloudObj[method]) {
    return Promise.reject({
      errno: -2,
      message: "调用的接口不存在！",
    });
  }
  return cloudObj[method](requestData).then((res) => {
    if (res.errno != 0) {
      uni.showToast({
        icon: "none",
        title: res.message,
      });
      return Promise.reject(res);
    }
    return Promise.resolve(res.data);
  });
}

export default {
  call,
};
